package com.e.dianshang.controller;

import com.e.dianshang.pojo.Order;
import com.e.dianshang.pojo.Pay;
import com.e.dianshang.pojo.StorageValue;
import com.e.dianshang.service.OrderService;
import com.e.dianshang.service.PayService;
import com.e.dianshang.service.StorageValueService;
import com.e.dianshang.utils.BusinessException;
import com.e.dianshang.utils.JsonResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author Tian同學
 * @Date 2020/11/9 17:58
 * @Version 1.0
 */
@RestController
@RequestMapping("/pay")
@Api(value = "提交订单", tags = "提交订单的api")
public class PayController {

    @Resource
    PayService payService;

    @Resource
    OrderService orderService;

    @Resource
    StorageValueService storageValueService;

    @PostMapping("/insertPay")
    @ApiOperation(value = "提交订单接口", httpMethod = "POST")
    public Map insertPay(@RequestBody @ApiParam(value = "提交订单传入的json信息 数组", required = true) Map map) throws BusinessException {

        JsonResult jsonResult = new JsonResult();
//        System.out.println("map"+map);
        List<Map> list = (List<Map>) map.get("list");

        /*for (int i=0;i<list.size();i++){
            Integer id = Integer.valueOf(list.get(i).get("oId").toString());
            orderService.delete(id);
        }*/

        for (Map map1 : list) {

            String id = map1.get("oId").toString();
            orderService.delete(Integer.valueOf(id));

            storageValueService.updateValue(map1);
        }
//        System.out.println("list"+list);


        int i = payService.insertPay(map);

        if (i > 0) {
            jsonResult.setCode("0");
            jsonResult.put("unique",map.get("unique"));
            jsonResult.setMsg("提交成功！！");
        } else {
            throw new BusinessException("提交失败！！");
        }
        return jsonResult.getValues();

    }

}
